function title_form_dialog(url) {
    
    var tips = $( ".validateTips" );

    $( "#title_form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Inserisci": function() {
                var bValid = true;
                $( '#title_name' ).removeClass( "ui-state-error" );

                bValid = bValid && checkLength( $( "#title_name" ), "Nome", 3, 255 );
                if ( bValid ) {
                    /* inserisce tramite AJAX le informazioni su una nuova 
                     * organizzazione ==== TODO: gestire errori
                     */
                    $.ajax({
                        url: url,
                        type: 'POST',                 
                        async: false,
                        data: {name: $( '#title_name' ).val()},
                        success: function(new_title_id) {
                            title_name = $( '#title_name' ).val();
                            // append del nuovo elemento alla select
                            $( '#EducationExperienceTitleId' )
                                    .append(
                                        $('<option></option>')
                                            .attr('value', new_title_id)
                                            .text(title_name)
                                    );
                        }
                    });   
                    $( this ).dialog( "close" );

               }
            },
            "Annulla": function() {
                    $( this ).dialog( "close" );
            }
        },
        close: function() {
                $( '#title_name' ).val( "" ).removeClass( "ui-state-error" );
        }
    });
    
}

function organization_form_dialog(url) {
    
    $( '#organization_region' ).change(function() {
            
            var region_id = $(this).val();
            if(region_id != '') {
                remove_all_options('PersonDistrictId', 'Selezionare la provincia');
                remove_all_options('PersonTownId', 'Selezionare la città');
                get_districts_list(region_id, get_districts_list_url, 'PersonDistrictId');
            }
                
            
    })
    
    $( "#organization_form" ).dialog({
            autoOpen: false,
            height: 600,
            width: 500,
            modal: true,
            buttons: {
                "Inserisci": function() {
                    var bValid = true;

                    bValid = bValid && checkLength( $( "#organization_name" ), "Nome", 3, 255 );
                    if ( bValid ) {
                        /* inserisce tramite AJAX le informazioni su una nuova 
                         * organizzazione ==== TODO: gestire errori
                         */
                        $.ajax({
                            url: url,
                            type: 'POST',
                            async: false,
                            data: {name: $( '#organization_name' ).val(), address: $( '#organization_address' ).val(),
                                   level: $( '#organization_level' ).val(), city_id: $( '#organization_city' ).val()},
                            success: function(new_organization_id) {
                                organization_name = $( '#organization_name' ).val();

                                $( '#EducationExperienceOrganizationId' )
                                        .append(
                                            $('<option></option>')
                                                .attr('value', new_organization_id)
                                                .text(organization_name)
                                        );
                            }
                        }); 
                        $( this ).dialog( "close" );
                    }
                },
                "Annulla": function() {
                        $( this ).dialog( "close" );
                }
            },
            close: function() {
                    $( '#organization_name, #organization_address, #organization_level, #organization_city' )
                        .val( "" ).removeClass( "ui-state-error" );
            }
        });
    
}

function position_form_dialog(url) {
       
   var tips = $( ".validateTips" );

   $( "#position_form" ).dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "Inserisci": function() {
                var bValid = true;
                $( '#position_name' ).removeClass( "ui-state-error" );

                bValid = bValid && checkLength( $( "#position_name" ), "Nome", 3, 255 );
                if ( bValid ) {
                    /* inserisce tramite AJAX le informazioni su una nuova 
                     * organizzazione ==== TODO: gestire errori
                     */
                    $.ajax({
                        url: url,
                        type: 'POST',                 
                        async: false,
                        data: {name: $( '#position_name' ).val()},
                        success: function(new_position_id) {
                            position_name = $( '#position_name' ).val();
                            // append del nuovo elemento alla select
                            $( '#EducationExperiencePositionId' )
                                    .append(
                                        $('<option></option>')
                                            .attr('value', new_position_id)
                                            .text(position_name)
                                    );
                        }
                    });   
                    $( this ).dialog( "close" );

               }
            },
            "Annulla": function() {
                    $( this ).dialog( "close" );
            }
        },
        close: function() {
                $( '#position_name' ).val( "" ).removeClass( "ui-state-error" );
        }
    });

}

